<?php

namespace app\common\controller;

use think\Controller;
use think\Exception;
use think\Model;
use think\Request;

//TODO 特定操作的权限
class BaseController extends Controller
{
    protected $model = null;
    protected $partName = null;
    protected $M;
    protected $verify=[];

    /**
     * @param Request $request
     * @throws \Exception
     */
    public function __construct(Request $request = null)
    {
        parent::__construct($request);
        $model = new $this->model;
        if ($model instanceof Model) {
            $this->M = $model;
        } else {
            throw new Exception('错误的model');
        }
    }

    public function _initialize()
    {
        if (!$this->checkLogin()) exit('未登录');
    }

    //登录验证
    protected function checkLogin()
    {
        if (!session('?name')) {
            $url = request()->domain() . $_SERVER['REQUEST_URI'];
            if (cookie('url_before_login')) {
                cookie('url_before_login', null);
            }
            cookie('url_before_login', $url, 3600);
            return false;
        } else {
            return true;
        }
    }

    //新增
    public function add($data)
    {
        try {
            return $this->M->allowField(true)->save($data);
        } catch (\Exception $e) {
//            var_dump($this->M->getLastSql());
            return $e->getMessage();
        }
    }

    /**
     * @return mixed
     * @throws \Exception
     * 获取
     */
    public function get()
    {
        try {
            $options = input();
            //TODO 数量过大时禁止全部抓取
            if (isset($options)) {
                $data = $this->M->where($options)->select();
            } else {
                $data = $this->M->select();
            }
            $a = [];
            foreach ($data as $value) {
                $a[] = $value->toArray();
            }
            return $a;
        } catch (\Exception $e) {
            return $e->getMessage();
        }

    }

    /**
     * @param array $option
     * @param array $data
     * @return mixed
     * @throws \Exception
     * 更新
     */
    public function edit($option, $data)
    {
        try {
            return $this->M->get($option)->data($data, true)->allowField(true)->save();
        } catch (\Exception $e) {
            return $e->getMessage();
        }
    }

    //删除
    public function del(int $id)
    {
        try {
            return $this->M->destroy($id);
        } catch (\Exception $e) {
            return $e->getMessage();
        }
    }

    //获取特定字段
    public function getCol($needle, $condition)
    {
        $data=$this->M->where($condition)->value($needle);
        try{
            return unserialize($data);
        }catch(\Exception $e){
            return $data;
        }
    }

    /* 获取数量
     * 接收数组
     * 可接收多个参数
     * [name=>aa,date=>bb]
     */
    public function count()
    {
        $options = $this->fixQueryWord(input());
        if (isset($options)) {
            $count = $this->M->where($options)->count();
        } else {
            $count = $this->M->count();
        }
        return $count;
    }

    /**
     * @return mixed
     * @throws \Exception
     * 分页查询
     */
    public function getPage()
    {
        try {
            $inputData = input();
            $default = ['page' => 1, 'perPage' => 10];
            $query = array_merge($default, $inputData);
            $page = $query['page'];
            $perPage = $query['perPage'];
            $start = $perPage * ($page - 1);
            if (isset($query['options'])) {
                $options = $this->fixQueryWord($query['options']);
            }
            if (isset($options)) {
                $data = $this->M->where($options)->limit($start, $perPage)->select();
            } else {
                $data = $this->M->limit($start, $perPage)->select();
            }
            $a = [];
            foreach ($data as $value) {
                $a[] = $value->toArray();
            }
            return $a;
        } catch (\Exception $e) {
            return $e->getMessage();
        }
    }

    //替换查询关键字
    protected function fixQueryWord(array $options)
    {
        $queryWords = ['likes' => 'like'];
        foreach ($options as $index => $option) {
            if (is_array($option)) {
                if (isset($queryWords[$option[0]])) {
                    $options[$index][0] = $queryWords[$option[0]];
                }
            }
        }
        return $options;
    }

}